#!/usr/bin/env bash

MINER_DIR="/os/miners/custom"
mkdir -p ${MINER_DIR}/downloads
cd $MINER_DIR

. /os/bin/colors

if [[ $# -le 0 ]]; then
	echo "Downloads and installs custom miner from a given URL"
	echo -e "Usage:  ${YELLOW}custom-get URL [-f]${NOCOLOR}"
	echo -e "URL http or https URL of tar.gz archive with custom miner"
	echo -e "-f  optional argument to force reinstall"
	exit 1
fi

url="$1"
force=
[[ $2 == "-f" ]] && force=1

basename=`basename -s .tar.gz "$url"`
archname=`basename "$url"`
archive="$MINER_DIR/downloads/$archname"

version=`echo "$basename" | awk -F '-' '{ print $NF }'`
miner=`echo "$basename" | sed 's/'-$version'$//'`

[[ -z "$miner" ]] &&
	echo "${RED}Unable to detect miner name from given URL${NOCOLOR}" &&
	exit 1

echo "Name:    ${BGREEN}$miner${NOCOLOR}"
echo "Version: ${YELLOW}$version${NOCOLOR}"
echo "URL:     ${PURPLE}$url${NOCOLOR}"
echo "linshi"

URLFILE="$MINER_DIR/downloads/${miner}.url"

# cleanup miner dir if url does not match
[[ -d $MINER_DIR/$miner && (! -f $URLFILE || $(< $URLFILE) != "$url") ]] && rm -rf $MINER_DIR/$miner

if [[ -f $archive ]]; then
	if [[ $force == 1 ]]; then
		rm $archive
		rm -rf $MINER_DIR/$miner
	elif [[ -d $MINER_DIR/$miner ]]; then
		echo "${CYAN}> Already installed${NOCOLOR}"
		exit 0
	fi
elif [[ -d $MINER_DIR/$miner ]]; then # no archive but directory exists
	echo "${YELLOW}> Removing previous installation${NOCOLOR}"
	rm -rf $MINER_DIR/$miner
fi

## Downloading
#[[ -f /usr/lib/x86_64-linux-gnu/libcurl-compat.so.3.0.0 ]] && export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:/hive/lib

cd $MINER_DIR/downloads

ready=0

if [[ -f $archive ]]; then
	gunzip -t $archive 2>/dev/null && ready=1 || ready=-1
fi

if [[ $ready -eq -1 ]]; then
	echo "${YELLOW}> $archname found locally, maybe incomplete, trying to continue download"
	echo "${CYAN}> Downloading $url${NOCOLOR}"
	wget -t 5 -T 20 -c "$url"
	[[ $? -ne 0 ]] && echo "${RED}Error downloading $url${NOCOLOR}" && exit 1
	gunzip -t $archive 2>/dev/null
	if [[ $? -ne 0 ]]; then
		echo "${YELLOW}> $archname is broken, removing${NOCOLOR}"
		rm -f $archive
		ready=0
	else
		ready=1
	fi
fi

if [[ $ready -eq 0 ]]; then
	echo "${CYAN}> Downloading $url${NOCOLOR}"
	wget -t 5 -T 20 -c "$url"
	[[ $? -ne 0 ]] && echo "${RED}Error downloading $url${NOCOLOR}" && exit 1
fi

cd $MINER_DIR
echo "${CYAN}> Unpacking $archname${NOCOLOR}"
tar -xzv -f $archive
if [[ $? -ne 0 ]]; then
	echo "${RED}Error extracting $archname${NOCOLOR}"
	echo "${YELLOW}> Removing downloaded $archname${NOCOLOR}"
	rm -f $archive
	exit 1
fi

chown -R user $MINER_DIR/$miner

# Fix old paths. Try to do this automatically
#CUSTOM_CONFIG_FILENAME=${CUSTOM_CONFIG_FILENAME/\/hive\/custom/\/hive\/miners\/custom}
#CUSTOM_LOG_BASENAME=${CUSTOM_LOG_BASENAME/\/hive\/custom/\/hive\/miners\/custom}
sed -i 's/\/hive\/custom/\/hive\/miners\/custom/g' $MINER_DIR/$miner/h-manifest.conf
sed -i 's/\/hive\/custom/\/hive\/miners\/custom/g' $MINER_DIR/$miner/h-config.sh
sed -i 's/\/hive\/custom/\/hive\/miners\/custom/g' $MINER_DIR/$miner/h-run.sh
sed -i 's/\/hive\/custom/\/hive\/miners\/custom/g' $MINER_DIR/$miner/h-stats.sh

## Add 666666
sed -i 's/\/hive\/miners/\/os\/miners/g' $MINER_DIR/$miner/h-manifest.conf
sed -i 's/\/hive\/miners/\/os\/miners/g' $MINER_DIR/$miner/h-config.sh
sed -i 's/\/hive\/miners/\/os\/miners/g' $MINER_DIR/$miner/h-run.sh
sed -i 's/\/hive\/miners/\/os\/miners/g' $MINER_DIR/$miner/h-stats.sh
## 兼容 apool 自动更新
if [[ -f $MINER_DIR/$miner/check_update.sh ]]; then
    sed -i 's/\/hive\/miners/\/os\/miners/g' $MINER_DIR/$miner/check_update.sh
    sed -i '/\/hive\/bin\/miner start/d' $MINER_DIR/$miner/check_update.sh
fi

sed -i "s|/hive-config/wallet.conf|${os_wallet_conf}|" "$MINER_DIR/$miner/h-run.sh"

## Add 666666

echo "$url" > $URLFILE
